﻿namespace TextFiles
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    class MaxAreaInMatrixOfFile
    {
        // 1.2.12.05. Write a program that reads a text file containing a square matrix of numbers and finds in
        // the matrix an area of size 2 x 2 with a maximal sum of its elements. The first line in the input file
        // contains the size of matrix N. Each of the next N lines contain N numbers separated by space.
        // The output should be a single number in a separate text file. Example:
        // 4
        // 2 3 3 4
        // 0 2 3 4  ->  17
        // 3 7 1 2
        // 4 3 3 2

        static void Main()
        {
            StreamReader sr = new StreamReader(@"../../example.txt");

            int n = int.Parse(sr.ReadLine());
            string[,] matrice = new string[n, n];

            using (sr)
            {
                string[] lines = new string[n];
                for (int i = 0; i < n; i++)
                {
                    lines[i] = sr.ReadLine();
                    string[] asdf = new string[n];
                    asdf = lines[i].Split(' ');
                    for (int j = 0; j < n; j++)
                    {
                        matrice[i, j] = asdf[j];
                    }
                }
            }
            
            int bestSum = 0;
            int beginXplatform = 0;
            int beginYplatform = 0;
            for (int row = 0; row < matrice.GetLength(0) - 1; row++)
            {
                int currentSum = 0;
                for (int col = 0; col < matrice.GetLength(1) - 1; col++)
                {
                    currentSum = 0;
                    currentSum += int.Parse(matrice[row + 1, col]);
                    currentSum += int.Parse(matrice[row, col]);
                    currentSum += int.Parse(matrice[row, col + 1]);
                    currentSum += int.Parse(matrice[row + 1, col + 1]);
                    if (currentSum > bestSum)
                    {
                        bestSum = currentSum;
                        beginXplatform = col;
                        beginYplatform = row;
                    }
                }
            }

            Console.WriteLine(bestSum);
        }
    }
}